package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.mapper.AnswerMapper;
import cn.wolfcode.trip.base.mapper.QuestionMapper;
import cn.wolfcode.trip.base.query.AnswerQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IAnswerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerServiceImpl implements IAnswerService{

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public PageInfo queryByUserId(QueryObject qo) {
        //分页
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        //查询某用户的所有回答
        List <Answer> list = answerMapper.selectByUserId(qo);

        return new PageInfo(list);
    }

    @Override
    public List<Answer> selectByUserId(Long userId) {
        return null;
    }

    @Override
    public PageInfo queryByQuestionId(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        //根据问题id查回答
        List<Answer> list = answerMapper.selectByQuestionId(qo);
        return new PageInfo(list);
    }
    //新增回答
    @Override
    public void save(Answer answer) {
        //需要更新该问题的状态
        questionMapper.updateIsAnswer(answer.getQuestion().getId());
        //问题回答数加一
        questionMapper.updateSum(answer.getQuestion().getId());
        answerMapper.insert(answer);
    }
}
